<?php
/**
 * Created by PhpStorm.
 * User: C。
 * Date: 2019/8/13
 * Time: 9:04
 */
namespace app\home\controller;
use app\common\model\Config as ConfigModel;
use think\Controller;

/**
 * Share 分享
 * Class Share
 * @package app\home\controller
 */
class Share extends Controller
{
    /**
     * [getIndex  获取分享参数]
     * @author [C.]
     * @return mixed
     */
    public function getIndex()
    {
        $model = new ConfigModel;
        $param = input('param.');
        $url = $param['url'];
        if(!empty($param['type'])){
            $appid = $model->findData('zh_appid','cname','cvalue');
            $appsecret = $model->findData('zh_secret','cname','cvalue');
            $appid = $appid['cvalue'];
            $appsecret = $appsecret['cvalue'];
            $result = $this->getWxJsConfig($appid,$appsecret,$url,$param['type']);
        }else{
            $appid = config('home.appid');
            $appsecret = config('home.secret');
            $result = $this->getWxJsConfig($appid,$appsecret,$url);
        }

        return admin_json(1,'获取成功',$result);
    }
    /**
     * [getIndex  获取分享参数]
     * @author [C.]
     * @return mixed
     */
    public function getWxJsConfig($appid,$appsecret,$url = '',$type = '')
    {
        //1 获取基础accessToken
        $token = $this->getAccessToken($appid,$appsecret,$url,$type);
        //2 获取jsapi_ticket
        $ticket = $this->getTicket($token,$type);
        //3 拼接权限数据
        $auto = array(
            'noncestr' => getRandomString(16),
            'jsapi_ticket' => $ticket,
            'timestamp' => time(),
            'url' => $url,
        );
        //签名
        $auto['signature'] = $this->getSign($auto);
        $auto['appid'] = $appid;
        return $auto;
    }
    /**
     * [getIndex  获取AccessToken]
     * @author [C.]
     * @return mixed
     */
    public function getAccessToken($appid,$appsecret,$type='')
    {
        $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$appsecret;
        //缓存文件的完整路径
        if(!empty($type)) {
            $cacheFile = dirname(__FILE__) . '/accessToken.cache1.php';
        }else{
            $cacheFile = dirname(__FILE__).'/accessToken.cache.php';
        }

        //判定缓存文件

            $res = $this->Curl($url);

            $accessToken = $res;

            //生成缓存文件
            file_put_contents($cacheFile,'<?php return ' .var_export($accessToken,true) . '?>');


        return $accessToken;
    }
    /**
     * [getIndex  获取jsapi_ticket]
     * @author [C.]
     * @return mixed
     */
    public function getTicket($accessToken,$type='')
    {
        $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$accessToken['access_token'].'&type=jsapi';
        //缓存文件的完整路径
        if(!empty($type)){
            $cacheFile = dirname(__FILE__).'/ticket.cache1.php';
        }else{
            $cacheFile = dirname(__FILE__).'/ticket.cache.php';
        }


            $res = $this->Curl($url);

            $ticket = $res;
            //生成缓存文件
            file_put_contents($cacheFile,'<?php return ' .var_export($ticket,true) . '?>');

        return $ticket['ticket'];
    }
    /**
     * [getSign  获取签名]
     * @author [C.]
     * @return mixed
     */
    public function getSign($param = array(),$key = '')
    {
        //将参数数组按照参数名ASCII码从小到大排序
        ksort($param);

        foreach($param as $k => $item){
            //剔除参数值为空的参数
            if(!empty($item)){
                $newArr[] = $k. '=' .$item;
            }
        }
        //使用&连接参数
        $string = implode('&',$newArr);
        if($key == ''){
            $sign = sha1($string);//加密
        }else{
            $str = $string.'&key='.$key;
            $sign = strtoupper(md5($str));
        }
        return $sign;
    }
    /**
     *
     * 通过access_token openid 从工作平台获取UserInfo
     * @return openid
     */
    public function Curl($url) {
        $ch = curl_init(); //初始化curl
        curl_setopt($ch, CURLOPT_TIMEOUT, 300); //设置超时
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        $res = curl_exec($ch); //运行curl，结果以jason形式返回
        $data = json_decode($res, true);
        curl_close($ch);

        return $data;
    }
}